package com.im.foodorderingsystem.task;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.im.foodorderingsystem.Login;
import com.im.foodorderingsystem.Registration;
import com.im.foodorderingsystem.Utility;

public class RegistrationTask extends AsyncTask<String, String, String>{

	
	Registration registrationActivity;
	Context context;
	
	public RegistrationTask(Context context) {
		this.registrationActivity=(Registration)context;
		this.context=context;
	}
	
	@Override
	protected String doInBackground(String... parameters) {
		
//		String URL = "http://10.0.3.2/foodorderingsystem/index.php?r=api/user/login&email="
//				+ parameters[0] + "&password=" + parameters[1];
		
		String URL = Utility.registerUrl
				+"&email="+parameters[0]
				+"&password="+parameters[1]
				+"&first_name="+parameters[2];		
		System.out.println("URL "+URL);
		String result = null;
		HttpGetRequest httpgetrequest=new HttpGetRequest(URL);
		result=httpgetrequest.send();
		
		return result;
	}
	
	@Override
	protected void onPostExecute(String result) {

		try {
			Log.d("register response", result);
			
			JSONObject reader = new JSONObject(result);
			String response = reader.getString("response");
			
			if (response.equals("true")) {
				
				Toast.makeText(context, "Registration successful please login", Toast.LENGTH_LONG).show();
				
				Intent intent = new Intent(registrationActivity, Login.class);				
				registrationActivity.startActivity(intent);
			}else{
				Toast.makeText(context, "Error . Please try again", Toast.LENGTH_LONG).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		

	}

}
